package net.hn.hnms.biz.process.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.process.domain.ShiftReporting;
import net.hn.hnms.biz.process.domain.vo.ShiftReportingVo;
import net.hn.hnms.biz.process.mapper.ShiftReportingMapper;
import net.hn.hnms.biz.process.service.ShiftReportingService;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.ServiceException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShiftReportingServiceImpl extends ServiceImpl<ShiftReportingMapper, ShiftReporting> implements ShiftReportingService {

    @Override
    public int insert(ShiftReporting shiftReporting) {
        return baseMapper.insert(shiftReporting);
    }

    @Override
    public void deleteBatch(List<String> idList) {
        baseMapper.deleteBatchIds(idList);
    }

    @Override
    public ResponsePage<ShiftReportingVo> selectPageList(ShiftReportingVo shiftReportingVo, PageQuery pageQuery) {
        Page<ShiftReporting> page = baseMapper.selectPage(pageQuery.build(), buildQuery(shiftReportingVo).orderByDesc(ShiftReporting::getReportingDate)
                .orderByDesc(ShiftReporting::getCreateTime));
        List<ShiftReporting> list = new ArrayList<>(page.getRecords());
        List<ShiftReportingVo> shiftReportingVos = BeanUtil.copyToList(list, ShiftReportingVo.class);
        return new ResponsePage<>(shiftReportingVos, page.getTotal());
    }

    @Override
    public ShiftReportingVo selectDetailById(String id) {
        ShiftReporting shiftReporting = baseMapper.selectById(id);
        if (shiftReporting != null) {
            return BeanUtil.copyProperties(shiftReporting, ShiftReportingVo.class);
        }
        throw new ServiceException("数据不存在");
    }

    /**
     * 构造查询条件
     *
     * @param req
     * @return LambdaQueryWrapper<ExecutionFootageOutput> 查询条件
     */
    private LambdaQueryWrapper<ShiftReporting> buildQuery(ShiftReportingVo req) {
        LambdaQueryWrapper<ShiftReporting> lqw = new LambdaQueryWrapper<ShiftReporting>()
                .eq(req.getReportingDate() != null, ShiftReporting::getReportingDate, req.getReportingDate())
                .eq(req.getShiftId() != null, ShiftReporting::getShiftId, req.getShiftId())
                .like(req.getShiftClass() != null, ShiftReporting::getShiftClass, req.getShiftClass())
                .like(req.getWorkFace() != null, ShiftReporting::getWorkFace, req.getWorkFace())
                .like(req.getWorkPlace() != null, ShiftReporting::getWorkPlace, req.getWorkPlace())
                .and(o -> o.eq(req.getMineCode() != null, ShiftReporting::getMineCode, req.getMineCode())
                        .or()
                        .eq(req.getCompanyCode() != null, ShiftReporting::getCompanyCode, req.getCompanyCode())
                );
        return lqw;
    }


}
